<?php

namespace app\admin\controller;

use app\admin\model\Look;
use think\Controller;
use think\Request;
use think\Db;
use app\admin\controller\Base;

class Looks extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $rel = Db::name('look')->paginate(8,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);
        return view('look/index', ['data'=>$rel]);
    }


    public function ajaxListAction(){

        $page = request()->param('apage');

        if (!empty($page)) {
            $rel = model('look')->paginate(8,false,[
                'type'=> 'Bootstrap',
                'var_page' => 'page',
                'page' => $page,
                'path'=>'javascript:AjaxPage([PAGE]);'

            ]);
            $page = $rel->render();
        }
        return json(['data'=>$rel,'page'=>$page]);

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('look/add');
    }

    public function add(Request $request)
    {
        $info = input('post.');
        $arr['title'] = $info['title'];
        $arr['status'] = $info['status'];
        $file = $request->file('look');
        if ($file){
            $res = $file->move(ROOT_PATH  . 'public/static/upload/look');
            if($res){

                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $arr['look'] = $res->getSaveName();

            }else{
                // 上传失败获取错误信息
                return  $file->getError();
            }
        }
        $arr['time'] = time();
        $look = new Look();
        $result = $look->save($arr);
        if ($result > 0){
            $this->success('添加成功', url('admin/index/index'));
        }
    }

    

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $look = Look::get($id);
        $look->title = $request->put('title');
        $look->status = $request->put('status');
        $look->time = time();
        $result = $look->save();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '修改成功';
        }else{
            $data['status'] = false;
            $data['message'] = '修改失败';
        }
        return json($data);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $look = Look::get($id);
        $result = $look->delete();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '删除成功';
        }else{
            $data['status'] = false;
            $data['message'] = '删除失败';
        }
        return json($data);
    }


}
